import 'package:dio/dio.dart';
// import 'package:flutter/material.dart';
import 'package:puppy_store/models/cart_model.dart';
import 'package:puppy_store/models/home_model.dart';
import 'package:puppy_store/service/requests.dart';

class CartApi {
  // 获取猜你喜欢商品
  static Future<List<GoodsModel>> getUserLike(int limit) async {
    Response response = await PuppyRequestManager().handleRequest(
      '/goods/relevant?limit=$limit',
      'GET',
    );
    List res = response.data['result'];

    // 转模型数据
    List<GoodsModel> categoryGoods = [];
    res.forEach((element) {
      categoryGoods.add(GoodsModel.fromJson(element));
    });
    return categoryGoods;
  }

  // 加入购物车方法
  static Future<Map> addCart(String skuId, int count) async {
    Map data = {'skuId': skuId, 'count': count};
    Response response = await PuppyRequestManager().handleRequest(
      '/member/cart',
      'POST',
      data: data,
    );

    Map res = response.data['result'];

    return res;
  }

  // 获取购物车列表方法
  static Future<CartModel> getCart() async {
    Response response = await PuppyRequestManager().handleRequest(
      '/member/cart/mutli',
      'GET',
    );
    Map res = response.data['result'];
    // 转模型
    CartModel cartModel = CartModel.fromjson(res);
    return cartModel;
  }

  // 修改购物车
  static Future<CartItemModel> updateCart(
    String id,
    int count,
    bool selected,
  ) async {
    String path = '/member/cart/$id';
    Map data = {'count': count, 'selected': selected};
    Response response = await PuppyRequestManager().handleRequest(
      path,
      'PUT',
      data: data,
    );
    Map res = response.data['result'];
    CartItemModel updateModel = CartItemModel.fromjson(res);
    return updateModel;
  }

  // 删除购物车
  static Future<dynamic> deleteCart(String id) async {
    String path = '/member/cart';
    Map data = {
      "ids": [id],
    };
    Response response = await PuppyRequestManager().handleRequest(
      path,
      'DELETE',
      data: data,
    );
    dynamic res = response.data['result'];

    return res;
  }

  // 全选购物车
  static Future<dynamic> selectAll(bool selected) async {
    Map data = {"selected": selected};
    Response response = await PuppyRequestManager().handleRequest(
      '/member/cart/selected',
      'PUT',
      data: data,
    );

    dynamic res = response.data['result'];
    return res;
  }

  // 获取购物车数量
  static Future<int> getCartTotalCount() async {
    Response response = await PuppyRequestManager().handleRequest(
      '/member/cart/count',
      'GET',
    );
    dynamic res = response.data['result'];

    int count = res['count'];
    return count;
  }
}
